home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / Guitar / RokNRoleApp.m < prev   
Encoding:
Text File  |  1992-07-28  |  19.7 KB  |  689 lines

  1. /**************************************************************************/
  2. /* This is RokNRoleApp.m, which builds and controls the instrument */
  3. /* Copyright (c)1992 - Rick A. Vander Kam - All rights reserved         */
  4. /* Please refer to the README file for additional documentation        */
  5. /**************************************************************************/
  6.  
  7. #import "RokNRoleApp.h"
  8. #import <math.h>
  9.  
  10. #define SRATE 22050.00  /* sampling rate */
  11. #define DLL    256  /* max delay line length */
  12. #define TWOPI 6.283185307
  13. #define MAGIC 1073741824.0  /* used for scaling noise */
  14. #define GROW 0.0001  /* for use as FracdelayUG flag */
  15. #define SHRINK -0.0001  /* for use as FracdelayUG flag */
  16. #define E2LENGTH 101.35  /* String lengths for various pitches */
  17. #define E1LENGTH 234.80
  18. #define B1LENGTH 146.63
  19. #define B2LENGTH 57.030
  20. #define A1LENGTH 168.31
  21. #define A2LENGTH 68.593
  22. #define D1LENGTH 119.66
  23. #define D2LENGTH 44.023
  24. #define G1LENGTH 193.84
  25.  
  26.     static int i,FBPathLength;
  27.     static double bb01,presLength1,presFrac1;
  28.     static double theta1,newLength1,phi1,fracLength1,intLength1,c01,newBb01;
  29.     static double bb02,presLength2,presFrac2;
  30.     static double theta2,newLength2,phi2,fracLength2,intLength2,c02,newBb02;
  31.     static double GlissSpeed,DistoThresh,FBGain;
  32.     static double DistoLevel,XCoeff,X2Coeff,X3Coeff;
  33.     static double FIR1A0,FIR1A1,FIR2A0,FIR2A1;
  34.  
  35.     DSPDatum rndArray[DLL];
  36.     id theOrch;
  37.     id theDelay,delayMemObj,firCell;
  38.     id theDelay2,delayMemObj2,firCell2;
  39.     id theDelay3,delayMemObj3;
  40.     id theDCBlock,theFIR,theDisto;
  41.     id theDCBlock2, theFIR2;
  42.     id theAdd, theAdd2, theAdd3;
  43.     id theMult;
  44.     id theMixer;
  45.     id theOut;
  46.     id fbpp,firoutpp,fir2outpp,addoutpp,add2outpp;
  47.     id mulin1pp,mulin2pp;
  48.     id firinpp,fir2inpp,dcboutpp,dcb2outpp,distinpp,distoutpp,outpp;
  49.     
  50.     
  51.     
  52. @implementation RokNRoleApp
  53.  
  54.     static void handleMKError(char *msg) {
  55.           if (!NXRunAlertPanel("RokNRole",msg,"OK","Quit",NULL,NULL))
  56.           [NXApp terminate:NXApp];
  57.     }
  58.  
  59.  
  60. - appDidInit:sender
  61. {
  62.  
  63. /* Take care of any MusicKit errors */
  64.     MKSetErrorProc(handleMKError);
  65.  
  66.         theOrch=[Orchestra new];
  67. /* Check DSP availability */
  68.     if(![theOrch open]) [theOrch error:"DSP unavailable."];
  69.  
  70. /* Allocate and hook up */
  71.  
  72.      /* Patchpoints -- names are mnemonics for unit generator connections */
  73.     fbpp=[theOrch allocPatchpoint:MK_yPatch];
  74.     firoutpp=[theOrch allocPatchpoint:MK_yPatch];
  75.     fir2outpp=[theOrch allocPatchpoint:MK_xPatch];
  76.     addoutpp=[theOrch allocPatchpoint:MK_yPatch];
  77.     add2outpp=[theOrch allocPatchpoint:MK_xPatch];
  78.     firinpp=[theOrch allocPatchpoint:MK_yPatch];
  79.     fir2inpp=[theOrch allocPatchpoint:MK_xPatch];
  80.     dcboutpp=[theOrch allocPatchpoint:MK_yPatch];
  81.     dcb2outpp=[theOrch allocPatchpoint:MK_xPatch];
  82.     mulin1pp=[theOrch allocPatchpoint:MK_xPatch];
  83.     mulin2pp=[theOrch allocPatchpoint:MK_yPatch];
  84.     distinpp=[theOrch allocPatchpoint:MK_yPatch];
  85.     distoutpp=[theOrch allocPatchpoint:MK_yPatch];
  86.     outpp=[theOrch allocPatchpoint:MK_yPatch];
  87.  
  88.      /* Delay memory objects for DelayUG and FracdelayUG unit generators */
  89.     firCell=[theOrch allocSynthData:MK_xData length:1];
  90.     firCell2=[theOrch allocSynthData:MK_yData length:1];
  91.     delayMemObj=[theOrch allocSynthData:MK_xData length:DLL];
  92.     delayMemObj2=[theOrch allocSynthData:MK_yData length:DLL];
  93.     delayMemObj3=[theOrch allocSynthData:MK_xData length:DLL];
  94.  
  95.      /* Unit generators */
  96.     theAdd=[theOrch allocUnitGenerator:[Add2UGyyy class]];  /* string loop adder */
  97.     theAdd2=[theOrch allocUnitGenerator:[Add2UGxyx class]];  /* string loop adder */
  98.     theAdd3=[theOrch allocUnitGenerator:[Add2UGyyx class]];  /* string outputs adder */
  99.     theMult=[theOrch allocUnitGenerator:[Mul2UGyxy class]];  /* for scaling feedback signal */
  100.     theDelay=[theOrch allocUnitGenerator:[FracdelayUGyyx class]];  /* string loop delay */
  101.     theFIR=[theOrch allocUnitGenerator:[Fir3UGyy class]];  /* string loop timbre filter */
  102.     theDCBlock=[theOrch allocUnitGenerator:[DcblockUGyy class]];  /* dc-blocking */
  103.     theFIR2=[theOrch allocUnitGenerator:[Fir3UGxx class]];  /* string loop timbre filter */
  104.     theDCBlock2=[theOrch allocUnitGenerator:[DcblockUGxx class]];  /* dc-blocking */
  105.     theDelay2=[theOrch allocUnitGenerator:[FracdelayUGxxy class]];  /* string loop delay */
  106.     theDisto=[theOrch allocUnitGenerator:[CubicnlUGyy class]];  /* distortion unit */
  107.     theMixer=[theOrch allocUnitGenerator:[Scl2add2UGyyy class]];  /* signal mixer */
  108.     theDelay3=[theOrch allocUnitGenerator:[DelayUGyyx class]];  /* feedback path */
  109.     theOut=[theOrch allocUnitGenerator:[Out2sumUGy class]];  /* stereo output */
  110.     
  111.      /* Make the connections */
  112.     [theOut idle];        /* Idle until power on */                                           
  113.     [theDisto setOutput:distoutpp];
  114.     [theDisto setInput:distinpp];
  115.     [theAdd3 setOutput:distinpp];
  116.     [theAdd3 setInput1:dcboutpp];
  117.     [theAdd3 setInput2:dcb2outpp];
  118.     [theDelay setInput:dcboutpp];
  119.     [theDelay setOutput:firinpp];
  120.     [theDelay setDelayMemory:delayMemObj];
  121.     [theDelay setFIRCell:firCell];
  122.     [theDelay2 setInput:dcb2outpp];
  123.     [theDelay2 setOutput:fir2inpp];
  124.     [theDelay2 setDelayMemory:delayMemObj2];
  125.     [theDelay2 setFIRCell:firCell2];
  126.     [theDelay3 setInput:outpp];
  127.     [theDelay3 setOutput:mulin2pp];
  128.     [theDelay3 setDelayMemory:delayMemObj3];
  129.     [theFIR setInput:firinpp];
  130.     [theFIR setOutput:firoutpp];
  131.     [theFIR2 setInput:fir2inpp];
  132.     [theFIR2 setOutput:fir2outpp];
  133.     [theDCBlock setInput:addoutpp];
  134.     [theDCBlock setOutput:dcboutpp];
  135.     [theDCBlock2 setInput:add2outpp];
  136.     [theDCBlock2 setOutput:dcb2outpp];
  137.     [theAdd2 setOutput:add2outpp];
  138.     [theAdd2 setInput1:fbpp];
  139.     [theAdd2 setInput2:fir2outpp];
  140.     [theAdd setOutput:addoutpp];
  141.     [theAdd setInput1:fbpp];
  142.     [theAdd setInput2:firoutpp];
  143.     [theMult setOutput:fbpp];
  144.     [theMult setInput1:mulin1pp];
  145.     [theMult setInput2:mulin2pp];
  146.     [theMixer setOutput:outpp];
  147.     [theMixer setInput1:distinpp];
  148.     [theMixer setInput2:distoutpp];
  149.  
  150. /* Initialize (default) everything */
  151.  
  152.     [theOrch setTimed:NO];
  153.     [theOrch setSamplingRate:SRATE];
  154.     [theDelay setBb0:0.99999];  
  155.     [theDelay setBb1:0.0];
  156.     [theDelay setChg:0.0];
  157.     [theDelay adjustLength:128];
  158.     presLength1=128.0;
  159.     bb01=0.99999;
  160.     [theDelay resetPointer];
  161.     [theDelay2 setBb0:0.99999];
  162.     [theDelay2 setBb1:0.0];
  163.     [theDelay2 setChg:0.0];
  164.     [theDelay2 adjustLength:128];
  165.     presLength2=128.0;
  166.     bb02=0.99999;
  167.     [theDelay2 resetPointer];
  168.     [theDelay3 adjustLength:200];
  169.     [theDelay3 resetPointer];
  170.     [delayMemObj setToConstant:0];
  171.     [delayMemObj2 setToConstant:0];
  172.     [delayMemObj3 setToConstant:0];
  173.     [theFIR setBb0:0.25];
  174.     [theFIR setBb1:0.50];
  175.     [theFIR setBb2:0.25];
  176.     [theFIR clear];
  177.     [theFIR2 setBb0:0.25];
  178.     [theFIR2 setBb1:0.50];
  179.     [theFIR2 setBb2:0.25];
  180.     [theFIR2 clear];
  181.     [theDCBlock setBb0:0.9988615];
  182.     [theDCBlock setBb1:-0.9988615];
  183.     [theDCBlock setAa1:0.997723];
  184.     [theDCBlock clear];
  185.     [theDCBlock2 setBb0:0.9988615];
  186.     [theDCBlock2 setBb1:-0.9988615];
  187.     [theDCBlock2 setAa1:0.997723];
  188.     [theDCBlock2 clear];
  189.     [theDisto setAa1:0.99999];
  190.     [theDisto setAa2:0.0];
  191.     [theDisto setAa3:-0.9999];
  192.     [theDisto setThr:0.01];
  193.     [mulin1pp setToConstant:DSPDoubleToFix24(0.03)];
  194.     [theMixer setScale1:0.0];
  195.     [theMixer setScale2:0.99999];
  196.     [theOut setBearing:0.0];
  197.     GlissSpeed=0.1;
  198.     DistoThresh=0.01;
  199.     DistoLevel=0.99999;
  200.  
  201. /* Start it up */
  202.  
  203.     [theFIR run];
  204.     [theDCBlock run];
  205.     [theDelay run];
  206.     [theFIR2 run];
  207.     [theDCBlock2 run];
  208.     [theDelay2 run];
  209.     [theDelay3 run];
  210.     [theAdd run];
  211.     [theAdd2 run];
  212.     [theAdd3 run];
  213.     [theDisto run];
  214.     [theMult run];
  215.     [theMixer run];
  216.     [theOut run];
  217.     
  218.     [theOrch run];
  219.  
  220.     [theOrch flushTimedMessages];
  221.  
  222.     return self;
  223. }
  224. - powerSwitch:sender
  225. {
  226. /* Patches the output to the "Sink" location when power switch is off */
  227.  
  228.      if ([sender state]==1) {
  229.         [theOut run];
  230.         [theOut setInput:outpp];
  231.     }
  232.     else {
  233.         [theOut idle];
  234.     }
  235.     [theOrch flushTimedMessages];
  236.         return self;
  237. }
  238.  
  239. - pluck:sender
  240. {
  241. /* 
  242. Plucking consists of loading up the two string loop delay memory objects with random noise, which is scaled to avoid premature clipping when the outputs are added together.
  243. */
  244.  
  245.     for (i=0; i<DLL; i++) {
  246.        rndArray[i] = DSPDoubleToFix24((double)random()/(2.0*MAGIC)-1.0);
  247.     }
  248.     [delayMemObj setData:rndArray length:DLL offset:0];
  249.     [delayMemObj2 setData:rndArray length:DLL offset:0];
  250.     [theOrch flushTimedMessages];
  251.     return self;
  252. }
  253.  
  254. - setFBGain:sender
  255. {
  256. /* Set multiplicative constant for scaling the fed-back output signal */
  257.  
  258.     FBGain=[sender doubleValue];
  259.     [fbgainf setDoubleValue:FBGain];
  260.     [fbgains setDoubleValue:FBGain];
  261.     [mulin1pp setToConstant:DSPDoubleToFix24(FBGain)];
  262.     [theOrch flushTimedMessages];
  263.     return self;
  264. }
  265.  
  266. - setPitch1:sender
  267. {
  268. /*
  269. Here is where the tuning takes place.  Much of what follows is necessary to handle glissandi.  For the mathematical explanation, refer to Sullivan (reference [2] in the README file).  Basically, the floating point value (length in samples) of the tuning slider is first split into its integer and fractional parts, and the fractional part is used to compute the coefficients for the two-point FIR filter built into the interpolated delay line.  If the change in length (from the previous value) is less than 1.0, a glissando will occur; otherwise, the new length is set immediately.
  270. */
  271.  
  272.      /* Get integer and fractional parts */
  273.     newLength1=[sender doubleValue];
  274.     [str1f setDoubleValue:newLength1];
  275.     theta1=TWOPI/newLength1;
  276.     intLength1=(int) rint(newLength1-0.50) - 1; 
  277.     fracLength1=newLength1 - 1.0 - (double) intLength1;
  278.     if(fracLength1>=1.0) {
  279.        intLength1++;
  280.        fracLength1-=1.0;
  281.     }
  282.      /* Compute interpolation coefficients */
  283.     phi1= -fracLength1*theta1;
  284.     if(fabs(phi1) > 0.0001) {
  285.        newBb01=1.0/(1.0-(1.0/(cos(theta1)+sin(theta1)/tan(phi1))));
  286.     }
  287.     else {
  288.        newBb01=0.99999;
  289.     }
  290.  
  291.      /* Test whether glissando required */
  292.     if((presLength1-newLength1>=1.0)||(presLength1-newLength1<=-1.0)) {
  293.         /* Here for no gliss */
  294.        [theDelay adjustLength:intLength1];
  295.        [theDelay resetPointer];
  296.        [theDelay setBb0:newBb01];
  297.        [theDelay setBb1:(0.99999-newBb01)];
  298.        [theOrch flushTimedMessages];
  299.     }
  300.     else {  /* Need to gliss */
  301.        if(newLength1>presLength1) {  /* Increasing length if true */
  302.           if(presFrac1>fracLength1) {  /* Must grow one sample if true */
  303.              [theDelay setChg:GROW];   /* signal that we need to grow */
  304.         /* Loop for "smooth" pitch transition, up to integer boundary */
  305.          for(c01=bb01; c01>0.0; c01-=GlissSpeed) {
  306.              [theDelay setBb0:c01];
  307.             [theDelay setBb1:(0.99999-c01)];
  308.             [theOrch flushTimedMessages];
  309.         }
  310.         /* Trigger the grow */
  311.         [theDelay setBb0:0.0];
  312.         [theDelay setBb1:0.99999];
  313.         [theOrch flushTimedMessages];
  314.         /* Following the grow, must update the integer part of the length */
  315.         [theDelay adjustLength:intLength1];
  316.         [theDelay setChg:0.0];  /* Disable more growing */
  317.         [theOrch flushTimedMessages];
  318.         /* After growing, switch from pure delay to zero delay & continue "smooth" transition */
  319.         for(c01=0.99999; c01>=newBb01; c01 -= GlissSpeed) {
  320.             [theDelay setBb0:c01];
  321.             [theDelay setBb1:(0.99999-c01)];
  322.             [theOrch flushTimedMessages];
  323.         }
  324.          }
  325.          else {  /* Increase but no grow */
  326.              for (c01=bb01; c01>=newBb01; c01 -= GlissSpeed) {
  327.               [theDelay setBb0:c01];
  328.             [theDelay setBb1:(0.99999-c01)];
  329.             [theOrch flushTimedMessages];
  330.              }
  331.           }
  332.        }
  333.        else {  /* Decreasing length */
  334.           if(presFrac1<fracLength1) {  /* Must shrink one sample if true */
  335.              [theDelay setChg:SHRINK];
  336.          for(c01=bb01; c01<0.99999; c01+=GlissSpeed) {
  337.              [theDelay setBb0:c01];
  338.             [theDelay setBb1:(0.99999-c01)];
  339.             [theOrch flushTimedMessages];
  340.         }
  341.         [theDelay setBb0:0.99999];
  342.         [theDelay setBb1:0.0];
  343.         [theOrch flushTimedMessages];
  344.         [theDelay adjustLength:intLength1];
  345.         [theDelay setChg:0.0];
  346.         [theOrch flushTimedMessages];
  347.         for(c01=0.0; c01<=newBb01; c01 += GlissSpeed) {
  348.             [theDelay setBb0:c01];
  349.             [theDelay setBb1:(0.99999-c01)];
  350.             [theOrch flushTimedMessages];
  351.         }
  352.           }
  353.           else {   /* Decrease but no shrink */
  354.              for (c01=bb01; c01<=newBb01; c01 += GlissSpeed) {
  355.               [theDelay setBb0:c01];
  356.             [theDelay setBb1:(0.99999-c01)];
  357.             [theOrch flushTimedMessages];
  358.              }
  359.           }
  360.        }
  361.     }
  362.     
  363.     /* Housekeeping */    
  364.     bb01=newBb01;
  365.     [theDelay setBb0:bb01]; /* Finish off -- set "exact" values just to be sure */
  366.     [theDelay setBb1:(1-bb01)]; 
  367.     [theOrch flushTimedMessages];
  368.     presFrac1=fracLength1;
  369.     presLength1=newLength1; 
  370.  
  371.     return self;
  372. }
  373.  
  374. - setFIR2A1:sender  /* Adjust coefficient "a1" for string 2 */
  375. {
  376.     FIR2A1=[sender doubleValue];
  377.     [fir2a1f setDoubleValue:FIR2A1];
  378.     [fir2a1s setDoubleValue:FIR2A1];
  379.     [theFIR2 setBb1:FIR2A1];
  380.     [theOrch flushTimedMessages];
  381.     return self;
  382. }
  383.  
  384. - setFIR2A0:sender   /* Adjust coefficient "a0" for string 2 */
  385. {
  386.     FIR2A0=[sender doubleValue];
  387.     [fir2a0f setDoubleValue:FIR2A0];
  388.     [fir2a0s setDoubleValue:FIR2A0];
  389.     [theFIR2 setBb0:FIR2A0];
  390.     [theFIR2 setBb2:FIR2A0];
  391.     [theOrch flushTimedMessages];
  392.     return self;
  393. }
  394.  
  395. - setPitch2:sender  /* Just like string 1 -- see above */
  396. {
  397.     newLength2=[sender doubleValue];
  398.     [str2f setDoubleValue:newLength2];
  399.     theta2=TWOPI/newLength2;
  400.     intLength2=(int) rint(newLength2-0.50) - 1; 
  401.     fracLength2=newLength2 - 1.0 - (double) intLength2;
  402.     if(fracLength2>=1.0) {
  403.        intLength2++;
  404.        fracLength2-=1.0;
  405.     }
  406.     phi2= -fracLength2*theta2;
  407.     if(fabs(phi2) > 0.0001) {
  408.        newBb02=1.0/(1.0-(1.0/(cos(theta2)+sin(theta2)/tan(phi2))));
  409.     }
  410.     else {
  411.        newBb02=0.99999;
  412.     }
  413.     if((presLength2-newLength2>=1.0)||(presLength2-newLength2<=-1.0)) {
  414.        [theDelay2 adjustLength:intLength2];
  415.        [theDelay2 resetPointer];
  416.        [theDelay2 setBb0:newBb02];
  417.        [theDelay2 setBb1:(0.99999-newBb02)];
  418.        [theOrch flushTimedMessages];
  419.     }
  420.     else {
  421.        if(newLength2>presLength2) {
  422.           if(presFrac2>fracLength2) {  /* Must grow one sample if true */
  423.              [theDelay2 setChg:GROW]; 
  424.          for(c02=bb02; c02>0.0; c02-=GlissSpeed) {
  425.              [theDelay2 setBb0:c02];
  426.             [theDelay2 setBb1:(0.99999-c02)];
  427.             [theOrch flushTimedMessages];
  428.         }
  429.         [theDelay2 setBb0:0.0];
  430.         [theDelay2 setBb1:0.99999];
  431.         [theOrch flushTimedMessages];
  432.         [theDelay2 adjustLength:intLength2];
  433.         [theDelay2 setChg:0.0];
  434.         [theOrch flushTimedMessages];
  435.         for(c02=0.99999; c02>=newBb02; c02 -= GlissSpeed) {
  436.             [theDelay2 setBb0:c02];
  437.             [theDelay2 setBb1:(0.99999-c02)];
  438.             [theOrch flushTimedMessages];
  439.         }
  440.          }
  441.          else {  /* Increase but no grow */
  442.              for (c02=bb02; c02>=newBb02; c02 -= GlissSpeed) {
  443.               [theDelay2 setBb0:c02];
  444.             [theDelay2 setBb1:(0.99999-c02)];
  445.             [theOrch flushTimedMessages];
  446.              }
  447.           }
  448.        }
  449.        else {
  450.           if(presFrac2<fracLength2) {  /* Must shrink one sample if true */
  451.              [theDelay2 setChg:SHRINK];
  452.          for(c02=bb02; c02<0.99999; c02+=GlissSpeed) {
  453.              [theDelay2 setBb0:c02];
  454.             [theDelay2 setBb1:(0.99999-c02)];
  455.             [theOrch flushTimedMessages];
  456.         }
  457.         [theDelay2 setBb0:0.99999];
  458.         [theDelay2 setBb1:0.0];
  459.         [theOrch flushTimedMessages];
  460.         [theDelay2 adjustLength:intLength2];
  461.         [theDelay2 setChg:0.0];
  462.         [theOrch flushTimedMessages];
  463.         for(c02=0.0; c02<=newBb02; c02 += GlissSpeed) {
  464.             [theDelay2 setBb0:c02];
  465.             [theDelay2 setBb1:(0.99999-c02)];
  466.             [theOrch flushTimedMessages];
  467.         }
  468.           }
  469.           else {   /* Decrease but no shrink */
  470.              for (c02=bb02; c02<=newBb02; c02 += GlissSpeed) {
  471.               [theDelay2 setBb0:c02];
  472.             [theDelay2 setBb1:(0.99999-c02)];
  473.             [theOrch flushTimedMessages];
  474.              }
  475.           }
  476.        }
  477.     }
  478.     
  479.     
  480.     bb02=newBb02;
  481.     [theDelay2 setBb0:bb02]; /* Finish off -- set "exact" values just to be sure */
  482.     [theDelay2 setBb1:(0.99999-bb02)]; 
  483.     [theOrch flushTimedMessages];
  484.     presFrac2=fracLength2;
  485.     presLength2=newLength2; 
  486.  
  487.     return self;
  488. }
  489.  
  490. - setFIR1A1:sender  /* Adjust coefficient "a1" for string 1 */
  491. {
  492.     FIR1A1=[sender doubleValue];
  493.     [fir1a1f setDoubleValue:FIR1A1];
  494.     [fir1a1s setDoubleValue:FIR1A1];
  495.     [theFIR setBb1:FIR1A1];
  496.     [theOrch flushTimedMessages];
  497.     return self;
  498. }
  499.  
  500. - setFIR1A0:sender  /* Adjust coefficient "a0" for string 1 */
  501. {
  502.     FIR1A0=[sender doubleValue];
  503.     [fir1a0f setDoubleValue:FIR1A0];
  504.     [fir1a0s setDoubleValue:FIR1A0];
  505.     [theFIR setBb0:FIR1A0];
  506.     [theFIR setBb2:FIR1A0];
  507.     [theOrch flushTimedMessages];
  508.     return self;
  509. }
  510.  
  511. - setDistoLevel:sender  /* set the mix of distorted and undistorted signal */
  512. {
  513.     DistoLevel=[sender doubleValue];
  514.     [theMixer setScale1:(0.99999-DistoLevel)*DistoThresh];
  515.     [theMixer setScale2:DistoLevel];
  516.     [theOrch flushTimedMessages];
  517.     return self;
  518. }
  519.  
  520. - setDistoCoeffX2:sender  /* set the "input squared" distortion coefficient */
  521. {
  522.     X2Coeff=[sender doubleValue];
  523.     [x2f setDoubleValue:X2Coeff];
  524.     [x2s setDoubleValue:X2Coeff];
  525.     [theDisto setAa2:X2Coeff];
  526.     [theOrch flushTimedMessages];
  527.     return self;
  528. }
  529.  
  530. - setDistoThresh:sender  /* set the hard clipping threshold in the distortion unit */
  531. {    
  532.     DistoThresh=[sender doubleValue];
  533.     [thrf setDoubleValue:DistoThresh];
  534.     [thrs setDoubleValue:DistoThresh];
  535.     [theDisto setThr:DistoThresh];
  536.     [theMixer setScale1:(0.99999-DistoLevel)*DistoThresh];
  537.     [theMixer setScale2:DistoLevel];
  538.     [theOrch flushTimedMessages];
  539.     return self;
  540. }
  541.  
  542. - setFBPathLength:sender  /* adjust the feedback path length */
  543. {
  544.     FBPathLength=[sender intValue];
  545.     [fbplf setIntValue:FBPathLength];
  546.     [fbpls setIntValue:FBPathLength];
  547.     [theDelay3 adjustLength:FBPathLength];
  548.     [theDelay3 resetPointer];
  549.     [theOrch flushTimedMessages];
  550.     return self;
  551. }
  552.  
  553. - setDistoCoeffX3:sender  /* set the "input cubed" distortion coefficient */
  554. {
  555.     X3Coeff=[sender doubleValue];
  556.     [x3f setDoubleValue:X3Coeff];
  557.     [x3s setDoubleValue:X3Coeff];
  558.     [theDisto setAa3:X3Coeff];
  559.     [theOrch flushTimedMessages];
  560.     return self;
  561. }
  562.  
  563. - setGlissSpeed:sender  /* adjust parameter for smoothness vs. speed of glissandi */
  564. {
  565.     GlissSpeed=[sender doubleValue];
  566.     [gsf setDoubleValue:GlissSpeed];
  567.     [gss setDoubleValue:GlissSpeed];
  568.     return self;
  569. }
  570.  
  571. - setDistoCoeffX:sender  /* set the "unmodified input" distortion coefficient */
  572. {
  573.     XCoeff=[sender doubleValue];
  574.     [xf setDoubleValue:XCoeff];
  575.     [xs setDoubleValue:XCoeff];
  576.     [theDisto setAa1:XCoeff];
  577.     [theOrch flushTimedMessages];
  578.     return self;
  579. }
  580.  
  581. - setPresetChord:sender  /* Tuning and plucking for preset chord buttons */
  582. {
  583.     if(sender==eButton) {
  584.         presLength1=E1LENGTH;
  585.         presLength2=B1LENGTH;
  586.     }
  587.     if(sender==aButton) {
  588.         presLength1=A1LENGTH;
  589.         presLength2=E2LENGTH;
  590.     }
  591.     if(sender==dButton) {
  592.         presLength1=D1LENGTH;
  593.         presLength2=A2LENGTH;
  594.     }
  595.     if(sender==gButton) {
  596.         presLength1=G1LENGTH;
  597.         presLength2=D1LENGTH;
  598.     }
  599.     [str1f setDoubleValue:presLength1];
  600.     [str1s setDoubleValue:presLength1];
  601.     [str2f setDoubleValue:presLength2];
  602.     [str2s setDoubleValue:presLength2];
  603.     theta1=TWOPI/presLength1;
  604.     intLength1=(int) rint(presLength1-0.50) - 1; 
  605.     presFrac1=presLength1 - 1.0 - (double) intLength1;
  606.     if(presFrac1>=1.0) {
  607.        intLength1++;
  608.        presFrac1-=1.0;
  609.     }
  610.     phi1= -presFrac1*theta1;
  611.     if(fabs(phi1) > 0.0001) {
  612.        bb01=1.0/(1.0-(1.0/(cos(theta1)+sin(theta1)/tan(phi1))));
  613.     }
  614.     else {
  615.        bb01=0.99999;
  616.     }
  617.     theta2=TWOPI/presLength2;
  618.     intLength2=(int) rint(presLength2-0.50) - 1; 
  619.     presFrac2=presLength2 - 1.0 - (double) intLength2;
  620.     if(presFrac2>=1.0) {
  621.        intLength2++;
  622.        presFrac2-=1.0;
  623.     }
  624.     phi2= -presFrac2*theta2;
  625.     if(fabs(phi2) > 0.0001) {
  626.        bb02=1.0/(1.0-(1.0/(cos(theta2)+sin(theta2)/tan(phi2))));
  627.     }
  628.     else {
  629.        bb02=0.99999;
  630.     }
  631.     [theDelay adjustLength:intLength1];
  632.     [theDelay resetPointer];
  633.     [theDelay setBb0:bb01];
  634.     [theDelay setBb1:(0.99999-bb01)];
  635.     [theDelay2 adjustLength:intLength2];
  636.     [theDelay2 resetPointer];
  637.     [theDelay2 setBb0:bb02];
  638.     [theDelay2 setBb1:(0.99999-bb02)];
  639.     [theOrch flushTimedMessages];
  640.     [self pluck:nil];    
  641.     return self;
  642. }
  643.  
  644. - terminate:sender  /* Shut everything down when exiting the program */
  645. {
  646.     [theOut idle];
  647.     [theOrch flushTimedMessages];
  648.     [theOut dealloc];
  649.     [theDelay dealloc];
  650.     [theDelay2 dealloc];
  651.     [theDelay3 dealloc];
  652.     [theDisto dealloc];
  653.     [theDCBlock dealloc];
  654.     [theFIR dealloc];
  655.     [theDCBlock2 dealloc];
  656.     [theFIR2 dealloc];
  657.     [theAdd dealloc];
  658.     [theAdd2 dealloc];
  659.     [theAdd3 dealloc];
  660.     [theMult dealloc];
  661.     [theMixer dealloc];
  662.     [firCell dealloc];    
  663.     [firCell2 dealloc];    
  664.     [firoutpp dealloc];
  665.     [firinpp dealloc];    
  666.     [fir2outpp dealloc];
  667.     [fir2inpp dealloc];    
  668.     [dcboutpp dealloc];
  669.     [dcb2outpp dealloc];    
  670.     [fbpp dealloc];    
  671.     [addoutpp dealloc];    
  672.     [add2outpp dealloc];    
  673.     [distinpp dealloc];
  674.     [distoutpp dealloc];
  675.     [mulin1pp dealloc];
  676.     [mulin2pp dealloc];    
  677.     [outpp dealloc];    
  678.     [delayMemObj dealloc];    
  679.     [delayMemObj2 dealloc];    
  680.     [delayMemObj3 dealloc];    
  681.     [theOrch flushTimedMessages];
  682.     
  683.     [theOrch close];
  684.     [theOrch free];
  685.  
  686.     return [super terminate:self];
  687. }
  688. @end
  689.